package jdbc;


import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class JDBCTest3 {
    public static void main(String[] args) throws SQLException {
        // 1. 创建 数据源对象
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("000000");

        // 2. 创建 连接
        Connection connection = dataSource.getConnection();
        System.out.println(connection);

        // 3. 构建 sql 语句
        //    主流方式 PreparedStatement
        String str = "insert into student values (2,'李四')";
        PreparedStatement statement = connection.prepareStatement(str);

        // 4. 将 sql 语句 发送到数据库上执行
        int n = statement.executeUpdate();
        System.out.println(n);

        // 5. 释放资源
        statement.close();
        connection.close();


    }
}
